package com.zhiche.lisa.bms.utils;

import com.google.common.collect.Lists;

import java.util.List;

/**
 * @author linbao
 * @date 2019-09-25
 */
public class BmsCollectionUtils {

    /**
     * 按照指令数量分隔数据
     *
     * @param source
     * @param len
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> subListByNum(List<T> source, int len) {
        if (source == null || source.size() == 0 || len < 1) {
            return null;
        }

        List<List<T>> result = Lists.newArrayList();
        int count = (source.size() + len - 1) / len;
        for (int i = 0; i < count; i++) {
            List<T> value = null;
            if ((i + 1) * len < source.size()) {
                value = source.subList(i * len, (i + 1) * len);
            } else {
                value = source.subList(i * len, source.size());
            }
            result.add(value);
        }
        return result;
    }
}
